<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>oaasvm.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>model&nbsp;</span>=&nbsp;<span class=defun_name>oaasvm</span>(<span class=defun_in>data,options</span>)<br>
<span class=h1>%&nbsp;OAASVM&nbsp;Multi-class&nbsp;SVM&nbsp;using&nbsp;One-Agains-All&nbsp;decomposition.</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;oaasvm(&nbsp;data&nbsp;)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;oaasvm(&nbsp;data,&nbsp;options)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;oaasvm(&nbsp;data&nbsp;)&nbsp;uses&nbsp;one-agains-all&nbsp;deconposition</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;to&nbsp;train&nbsp;the&nbsp;multi-class&nbsp;Support&nbsp;Vector&nbsp;Machines&nbsp;(SVM)</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;classifier.&nbsp;The&nbsp;classification&nbsp;into&nbsp;nclass&nbsp;classes&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;is&nbsp;decomposed&nbsp;to&nbsp;nclass&nbsp;binary&nbsp;problems.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;oaasvm(&nbsp;data,&nbsp;options)&nbsp;allows&nbsp;to&nbsp;specify&nbsp;the</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;binary&nbsp;SVM&nbsp;solver&nbsp;and&nbsp;its&nbsp;paramaters.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;[struct]&nbsp;Training&nbsp;data:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Training&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Labels&nbsp;of&nbsp;training&nbsp;data&nbsp;(1,2,...,nclass).&nbsp;</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;[struct]&nbsp;Control&nbsp;parameters:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.bin_svm&nbsp;[string]&nbsp;Function&nbsp;which&nbsp;implements&nbsp;the&nbsp;binary&nbsp;SVM&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;solver;&nbsp;(default&nbsp;'smo').</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.verb&nbsp;[1x1]&nbsp;If&nbsp;1&nbsp;then&nbsp;a&nbsp;progress&nbsp;info&nbsp;is&nbsp;displayed&nbsp;(default&nbsp;0).</span><br>
<span class=help>%&nbsp;&nbsp;The&nbsp;other&nbsp;fileds&nbsp;of&nbsp;options&nbsp;specifies&nbsp;the&nbsp;options&nbsp;of&nbsp;the&nbsp;binary</span><br>
<span class=help>%&nbsp;&nbsp;solver&nbsp;(e.g.,&nbsp;ker,&nbsp;arg,&nbsp;C).&nbsp;See&nbsp;help&nbsp;of&nbsp;the&nbsp;selected&nbsp;solver.</span><br>
<span class=help>%&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Multi-class&nbsp;SVM&nbsp;classifier:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Alpha&nbsp;[nsv&nbsp;x&nbsp;nclass]&nbsp;Weights&nbsp;(Lagrangians).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.b&nbsp;[nclass&nbsp;x&nbsp;1]&nbsp;Biases&nbsp;of&nbsp;discriminant&nbsp;functions.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.sv.X&nbsp;[dim&nbsp;x&nbsp;nsv]&nbsp;Support&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.nsv&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;support&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.trnerr&nbsp;[1x1]&nbsp;Training&nbsp;error.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.kercnt&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;kernel&nbsp;evaluations.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.options&nbsp;[struct[&nbsp;Copy&nbsp;of&nbsp;input&nbsp;argument&nbsp;options.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;=&nbsp;load('pentagon');</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;=&nbsp;struct('ker','rbf','arg',1,'C',10,'verb',1);</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;oaasvm(data,options);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;ppatterns(data);&nbsp;ppatterns(&nbsp;model.sv.X,&nbsp;'ok',13);</span><br>
<span class=help>%&nbsp;&nbsp;pboundary(&nbsp;model&nbsp;);</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;SVMCLASS,&nbsp;OAOSVM.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2005,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;25-jan-2005,&nbsp;VF,&nbsp;option&nbsp;solver&nbsp;replaced&nbsp;by&nbsp;bin_svm&nbsp;</span><br>
<span class=help1>%&nbsp;27-may-2004,&nbsp;VF,&nbsp;completely&nbsp;re-programed</span><br>
<span class=help1>%&nbsp;18-sep-2001,&nbsp;V.&nbsp;Franc,&nbsp;created</span><br>
<br>
<hr>
<span class=comment>%&nbsp;Process&nbsp;inputs</span><br>
<span class=comment>%-----------------------------</span><br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;2,&nbsp;options&nbsp;=&nbsp;[];&nbsp;<span class=keyword>else</span>&nbsp;options=c2s(options);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'verb'</span>),&nbsp;options.verb&nbsp;=&nbsp;0;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'bin_svm'</span>),&nbsp;options.bin_svm&nbsp;=&nbsp;<span class=quotes>'smo'</span>;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'ker'</span>),&nbsp;options.ker&nbsp;=&nbsp;<span class=quotes>'linear'</span>;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'arg'</span>),&nbsp;options.arg&nbsp;=&nbsp;1;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'C'</span>),&nbsp;options.C&nbsp;=&nbsp;inf;&nbsp;<span class=keyword>end</span><br>
<br>
[dim,num_data]&nbsp;=&nbsp;size(data.X);<br>
nclass&nbsp;=&nbsp;max(data.y);<br>
<br>
<span class=comment>%&nbsp;display&nbsp;info</span><br>
<span class=comment>%---------------------</span><br>
<span class=keyword>if</span>&nbsp;options.verb&nbsp;==&nbsp;1,<br>
&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Binary&nbsp;rules:&nbsp;%d\n'</span>,&nbsp;nclass);<br>
&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Training&nbsp;data:&nbsp;%d\n'</span>,&nbsp;num_data);<br>
&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Dimension:&nbsp;%d\n'</span>,&nbsp;dim);<br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(&nbsp;options,&nbsp;<span class=quotes>'ker'</span>),&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Kernel:&nbsp;%s\n'</span>,&nbsp;options.ker);&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(&nbsp;options,&nbsp;<span class=quotes>'arg'</span>),&nbsp;<span class=io>fprintf</span>(<span class=quotes>'arg:&nbsp;%f\n'</span>,&nbsp;options.arg(1));&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(&nbsp;options,&nbsp;<span class=quotes>'C'</span>),&nbsp;<span class=io>fprintf</span>(<span class=quotes>'C:&nbsp;%f\n'</span>,&nbsp;options.C);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>end</span><br>
<br>
<span class=comment>%----------------------------------------</span><br>
Alpha&nbsp;=&nbsp;zeros(num_data,nclass);<br>
b&nbsp;=&nbsp;zeros(nclass,1);<br>
orig_labels&nbsp;=&nbsp;data.y;<br>
kercnt&nbsp;=&nbsp;0;<br>
<br>
<span class=comment>%&nbsp;One-Against-All&nbsp;decomposition</span><br>
<span class=comment>%----------------------------------------</span><br>
<span class=keyword>for</span>&nbsp;i=1:nclass,<br>
<br>
&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;options.verb==1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Training&nbsp;rule&nbsp;%d'</span>,&nbsp;i);<br>
&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
<br>
&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;set&nbsp;binary&nbsp;subtask</span><br>
&nbsp;&nbsp;&nbsp;<span class=comment>%---------------------------------------------</span><br>
&nbsp;&nbsp;&nbsp;bin_labels&nbsp;=&nbsp;zeros(1,num_data);<br>
&nbsp;&nbsp;&nbsp;bin_labels(find(&nbsp;orig_labels==i))&nbsp;=&nbsp;1;<br>
&nbsp;&nbsp;&nbsp;bin_labels(find(&nbsp;orig_labels~=i))&nbsp;=&nbsp;2;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;data.y&nbsp;=&nbsp;bin_labels;<br>
<br>
&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;solve&nbsp;binary&nbsp;subtask</span><br>
&nbsp;&nbsp;&nbsp;<span class=comment>%-------------------------------------</span><br>
&nbsp;&nbsp;&nbsp;bin_model&nbsp;=&nbsp;<span class=eval>feval</span>(&nbsp;options.bin_svm,&nbsp;data,&nbsp;options&nbsp;);<br>
<br>
&nbsp;&nbsp;&nbsp;Alpha(bin_model.sv.inx,i)&nbsp;=&nbsp;bin_model.Alpha(:);<br>
&nbsp;&nbsp;&nbsp;b(i)&nbsp;=&nbsp;bin_model.b;<br>
&nbsp;&nbsp;&nbsp;kercnt&nbsp;=&nbsp;kercnt&nbsp;+&nbsp;bin_model.kercnt;<br>
&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;progress&nbsp;info&nbsp;</span><br>
&nbsp;&nbsp;&nbsp;<span class=comment>%----------------------------</span><br>
&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;options.verb&nbsp;==1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(bin_model,&nbsp;<span class=quotes>'trnerr'</span>),<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>':&nbsp;trnerr&nbsp;=&nbsp;%.4f'</span>,&nbsp;bin_model.trnerr);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(bin_model,&nbsp;<span class=quotes>'margin'</span>),<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>',&nbsp;margin&nbsp;=&nbsp;%f'</span>,&nbsp;bin_model.margin&nbsp;);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'\n'</span>);<br>
&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;set&nbsp;output&nbsp;model</span><br>
<span class=comment>%---------------------------------</span><br>
<br>
<span class=comment>%&nbsp;indices&nbsp;of&nbsp;all&nbsp;support&nbsp;vectors</span><br>
inx&nbsp;=&nbsp;find(sum(abs(Alpha),2)~=&nbsp;0);<br>
<br>
model.Alpha&nbsp;=&nbsp;Alpha(inx,:);<br>
model.b&nbsp;=&nbsp;b;<br>
model.sv.X&nbsp;=&nbsp;data.X(:,inx);<br>
model.sv.y&nbsp;=&nbsp;orig_labels(inx);<br>
model.sv.inx&nbsp;=&nbsp;inx;<br>
model.nsv&nbsp;=&nbsp;length(inx);<br>
model.kercnt&nbsp;=&nbsp;kercnt;<br>
model.options&nbsp;=&nbsp;options;<br>
model.fun&nbsp;=&nbsp;<span class=quotes>'svmclass'</span>;<br>
model.trnerr&nbsp;=&nbsp;cerror(&nbsp;svmclass(data.X,&nbsp;model),&nbsp;orig_labels&nbsp;);<br>
<br>
<span class=keyword>if</span>&nbsp;strcmp(options.ker,<span class=quotes>'linear'</span>)&nbsp;==&nbsp;1,<br>
&nbsp;&nbsp;model.W&nbsp;=&nbsp;model.sv.X*model.Alpha;<br>
<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;display&nbsp;info</span><br>
<span class=comment>%--------------------</span><br>
<span class=keyword>if</span>&nbsp;options.verb&nbsp;==&nbsp;1,<br>
&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Total&nbsp;training&nbsp;error&nbsp;=&nbsp;%.4f\n'</span>,&nbsp;model.trnerr);<br>
<span class=keyword>end</span><br>
<br>
<span class=jump>return</span>;<br>
<span class=comment>%&nbsp;EOF</span><br>
</code>
